# converters/snap.py
import subprocess
import yaml

def convert_snap(wsl_file_path, temp_dir, log_callback):
    """将 Snap 包转换为玲珑包"""
    log_callback("开始转换 Snap 包...\n")
    
    # Step 1: 解压 Snap 包
    extract_dir = f"{temp_dir}/snap-extract"
    subprocess.run(f"wsl unsquashfs -d {extract_dir} {wsl_file_path}", shell=True, check=True)
    
    # Step 2: 解析 snap.yaml
    with open(f"{extract_dir}/meta/snap.yaml") as f:
        snap_meta = yaml.safe_load(f)
    
    # Step 3: 生成 linglong.yaml
    yaml_content = f"""
    package:
      id: com.snap.converted.{snap_meta['name']}
      version: {snap_meta['version']}
      runtime: org.deepin.Runtime/20/x86_64
      base: minimal
    files:
      - from: {extract_dir}/
        to: /
    dependencies:
      - runtime: org.deepin.Runtime/20/x86_64
    """
    return yaml_content